import 'package:get/get.dart';
import 'package:rjpb/page/login/apis/auth_service.dart';
import 'package:rjpb/utils/toast_utils.dart';
import 'package:flutter/material.dart';
import 'package:rjpb/page/home/fireAlarm/controller/fireAlarm_page_controller.dart';

class LoginController extends GetxController {
  final username = ''.obs;
  final password = ''.obs;
  final isLoading = false.obs;

  Future<void> handleLogin(BuildContext context) async {
    isLoading.value = true;
    try {
      final success = await AuthService.login(username.value, password.value);
      if (success) {
        showCustomToast(context, '登录成功！', ToastType.success);
        // 加载火警页面数据
        final fireAlarmController = Get.put(FireAlarmPageController());
        await fireAlarmController.loadTabData("火警");
        Get.offNamed('/home'); // 登录成功跳转到首页
      } else {
        showCustomToast(context, '登录失败，请检查用户名或密码', ToastType.error);
      }
    } catch (e) {
      showCustomToast(context, '登录请求失败: $e', ToastType.error);
    } finally {
      isLoading.value = false;
    }
  }
}
